/*
 * Copyright (c) 2011-2014, fortiss GmbH.
 * Licensed under the Apache License, Version 2.0.
 *
 * Use, modification and distribution are subject to the terms specified
 * in the accompanying license file LICENSE.txt located at the root directory
 * of this software distribution. A copy is available at
 * http://chromosome.fortiss.org/.
 *
 * This file is part of CHROMOSOME.
 *
 * $Id: plugAndPlayManagerProcessRemoveComponentRequestFunction.c 7837 2014-03-14 12:33:13Z wiesmueller $
 */

/**
 * \file
 *         Source file for function pnpManagerProcessRemoveComponentRequest in component pnpManager.
 *
 * \author
 *         This file has been generated by the CHROMOSOME Modeling Tool (XMT)
 *         (fortiss GmbH).
 */

/******************************************************************************/
/***   Includes                                                             ***/
/******************************************************************************/
#include "xme/core/plugAndPlay/include-gen/plugAndPlayManagerProcessRemoveComponentRequestFunction.h"

#include "xme/core/plugAndPlay/include-gen/plugAndPlayManagerProcessRemoveComponentRequestFunctionWrapper.h"
#include "xme/core/plugAndPlay/include-gen/plugAndPlayManagerComponent.h"
#include "xme/core/plugAndPlay/include-gen/plugAndPlayManagerComponentWrapper.h"
#include "xme/core/plugAndPlay/include-gen/pnpManagerManifest.h"

#include "xme/core/logUtils.h"

#include "xme/hal/include/mem.h"

// PROTECTED REGION ID(XME_CORE_PNP_PNPMANAGERPROCESSREMOVECOMPONENTREQUESTFUNCTION_C_INCLUDES) ENABLED START
#include "xme/core/plugAndPlay/include/plugAndPlayManager.h"
// PROTECTED REGION END

/******************************************************************************/
/***   Definitions                                                          ***/
/******************************************************************************/

// PROTECTED REGION ID(XME_CORE_PNP_PNPMANAGERPROCESSREMOVECOMPONENTREQUESTFUNCTION_C_DEFINITIONS) ENABLED START

// PROTECTED REGION END

/******************************************************************************/
/***   Variables                                                            ***/
/******************************************************************************/

// PROTECTED REGION ID(XME_CORE_PNP_PNPMANAGERPROCESSREMOVECOMPONENTREQUESTFUNCTION_C_VARIABLES) ENABLED START

// PROTECTED REGION END

/******************************************************************************/
/***   Prototypes                                                           ***/
/******************************************************************************/

// PROTECTED REGION ID(XME_CORE_PNP_PNPMANAGERPROCESSREMOVECOMPONENTREQUESTFUNCTION_C_PROTOTYPES) ENABLED START
/**
 * \brief Sends to Plug and Play Manager a component removal request for a given node. 
 *
 * \param[in] nodeID The node identifier in which is installed the component. 
 * \param[in] componentID The component identifier to remove. 
 *
 * \retval XME_STATUS_SUCCESS if the remove operation was processed successfully, 
 *         and remove runtime graphs were generated to be processed by
 *         the process runtime graphs function. 
 * \retval XME_STATUS_INVALID_PARAMETER if the provided node ID is an invalid node ID. 
 * \retval XME_STATUS_INTERNAL_ERROR if cannot process successfully the component removal request. 
 */
xme_status_t
processRemoveComponentRequest
(
    xme_core_node_nodeId_t nodeID,
    xme_core_component_t componentID
);

/**
 * \brief Removes from the component repository the component instance. 
 *
 * \param[in] nodeID The node identifier in which is installed the component. 
 * \param[in] componentID The component identifier to remove. 
 *
 * \retval XME_STATUS_SUCCESS if the component instance was successfully removed. 
 * \retval XME_STATUS_INVALID_PARAMETER if the provided parameters are not valid parameters. 
 * \retval XME_STATUS_NOT_FOUND if there is not registered in the component repository the
 *         instance corresponding to the input parameters. 
 */
xme_status_t
removeComponentInstance
(
    xme_core_node_nodeId_t nodeID,
    xme_core_component_t componentID
);
// PROTECTED REGION END

/******************************************************************************/
/***   Implementation                                                       ***/
/******************************************************************************/
xme_status_t
xme_core_pnp_pnpManagerProcessRemoveComponentRequestFunction_init
(
    xme_core_pnp_pnpManagerComponent_config_t* const componentConfig
)
{
    // PROTECTED REGION ID(XME_CORE_PNP_PNPMANAGERPROCESSREMOVECOMPONENTREQUESTFUNCTION_INITIALIZE_C) ENABLED START
    
    // Nothing to do here
    
    XME_UNUSED_PARAMETER(componentConfig);
    
    return XME_STATUS_SUCCESS;
    
    // PROTECTED REGION END
}

void
xme_core_pnp_pnpManagerProcessRemoveComponentRequestFunction_step
(
    xme_core_pnp_pnpManagerComponent_config_t* const componentConfig
)
{
    xme_status_t status[1];
    
    xme_core_topic_pnp_removeComponentRequest_t portInRemoveComponentRequestData; // Required port.
    
    (void)xme_hal_mem_set(&portInRemoveComponentRequestData, 0u, sizeof(xme_core_topic_pnp_removeComponentRequest_t));
    
    status[0] = xme_core_pnp_pnpManagerComponentWrapper_readPortInRemoveComponentRequest(&portInRemoveComponentRequestData);
    
    {
        // PROTECTED REGION ID(XME_CORE_PNP_PNPMANAGERPROCESSREMOVECOMPONENTREQUESTFUNCTION_STEP_C) ENABLED START
        XME_UNUSED_PARAMETER(componentConfig);

        XME_CHECK_MSG
        (
            XME_STATUS_SUCCESS == status[0] ||
            XME_STATUS_NO_SUCH_VALUE == status[0],
            XME_CHECK_RVAL_VOID,
            XME_LOG_WARNING,
            "[PnPManager] Cannot read the input remove component request.\n"
        );

        if (XME_STATUS_SUCCESS == status[0])
        {
            // There is data in the input remove component request.
            XME_CHECK_MSG
            (
                XME_STATUS_SUCCESS == processRemoveComponentRequest(portInRemoveComponentRequestData.nodeId, portInRemoveComponentRequestData.componentId),
                XME_CHECK_RVAL_VOID,
                XME_LOG_WARNING,
                "[PnPManager] Cannot process the remove component request (nodeId=%d,componentId=%d).\n",
                portInRemoveComponentRequestData.nodeId,
                portInRemoveComponentRequestData.componentId
            );

            XME_CHECK_MSG
            (
                XME_STATUS_SUCCESS == removeComponentInstance(portInRemoveComponentRequestData.nodeId, portInRemoveComponentRequestData.componentId),
                XME_CHECK_RVAL_VOID,
                XME_LOG_WARNING,
                "[PnPManager] Cannot remove component instance from component repository (nodeId=%d,componentId=%d).\n",
                portInRemoveComponentRequestData.nodeId,
                portInRemoveComponentRequestData.componentId
            );
        }
        // PROTECTED REGION END
    }
    
    {
        // PROTECTED REGION ID(XME_CORE_PNP_PNPMANAGERPROCESSREMOVECOMPONENTREQUESTFUNCTION_STEP_2_C) ENABLED START
        
        // TODO: Auto-generated stub
        //       Check return values of writePort calls here
        
        // PROTECTED REGION END
    }
}

void
xme_core_pnp_pnpManagerProcessRemoveComponentRequestFunction_fini
(
    xme_core_pnp_pnpManagerComponent_config_t* const componentConfig
)
{
    // PROTECTED REGION ID(XME_CORE_PNP_PNPMANAGERPROCESSREMOVECOMPONENTREQUESTFUNCTION_TERMINATE_C) ENABLED START
    
    // Nothing to do here
    
    XME_UNUSED_PARAMETER(componentConfig);
    
    // PROTECTED REGION END
}

// PROTECTED REGION ID(XME_CORE_PNP_PNPMANAGERPROCESSREMOVECOMPONENTREQUESTFUNCTION_IMPLEMENTATION_C) ENABLED START
xme_status_t
processRemoveComponentRequest
(
    xme_core_node_nodeId_t nodeID,
    xme_core_component_t componentID
)
{
    xme_status_t unannounceComponentStatus;

    unannounceComponentStatus = xme_core_pnp_pnpManager_unannounceComponentOnNode(nodeID, componentID);

    XME_CHECK_MSG
    (
        XME_STATUS_SUCCESS == unannounceComponentStatus,
        unannounceComponentStatus,
        XME_LOG_ERROR,
        "[PnPManager] Cannot unannounce component (NodeID=%d,ComponentID=%d).\n", nodeID, componentID
    );

    return XME_STATUS_SUCCESS;
}

xme_status_t
removeComponentInstance
(
    xme_core_node_nodeId_t nodeID,
    xme_core_component_t componentID
)
{
    xme_status_t removeComponentInstanceStatus;

    removeComponentInstanceStatus = xme_core_pnp_pnpManager_removeComponentInstance(nodeID, componentID);

    XME_CHECK_MSG
    (
        XME_STATUS_SUCCESS == removeComponentInstanceStatus,
        removeComponentInstanceStatus,
        XME_LOG_ERROR,
        "[PnPManager] Cannot remove component instance (NodeID=%d,ComponentID=%d).\n", nodeID, componentID
    );

    return XME_STATUS_SUCCESS;
}
// PROTECTED REGION END
